package com.yx.front.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yx.front.config.global.BizException;
import com.yx.front.config.global.CommonResult;
import com.yx.front.converter.CommentsInfoConverter;
import com.yx.front.model.CommentsInfo;
import com.yx.front.model.SysUser;
import com.yx.front.model.param.CommentsInfoParam;
import com.yx.front.model.param.CommentsParam;
import com.yx.front.model.vo.CommentsInfoVo;
import com.yx.front.service.CommentsInfoService;
import com.yx.front.service.SysUserService;
import com.yx.front.utils.LoginUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author YangXiong
 */
@Component
public class CommentsInfoManager {
    @Resource
    private CommentsInfoService commentsInfoService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private LoginUtil loginUtil;
    @Resource
    private CommentsInfoConverter commentsInfoConverter;

    public CommonResult<List<CommentsInfoVo>> getPageByMainId(CommentsParam param) {
        if(Objects.isNull(param)){
            throw new BizException("参数不能为空");
        }
        Page<CommentsInfo> infoPage = new Page<>(param.getCurrentPage(), param.getTotalPage());
        LambdaQueryWrapper<CommentsInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CommentsInfo::getOwnerId, param.getMainId());
        Page<CommentsInfo> page = commentsInfoService.page(infoPage, lambdaQueryWrapper);
        List<CommentsInfoVo> result = commentsInfoConverter.handlerResult(page.getRecords());
        return CommonResult.success(result, page.getTotal());
    }

    public Boolean insert(CommentsInfoParam param) {
        if(Objects.isNull(param)){
            throw new BizException("非法操作，参数不能为空");
        }
        SysUser user = loginUtil.getCurrentUser();
        CommentsInfo commentsInfo = createCommentsInfo(user, param);
        return commentsInfoService.save(commentsInfo);
    }

    private CommentsInfo createCommentsInfo(SysUser user, CommentsInfoParam param) {
        CommentsInfo info = new CommentsInfo();
        info.setCreateTime(DateUtil.date());
        info.setUpdateTime(DateUtil.date());
        info.setContent(param.getContent());
        info.setFromId(user.getId());
        info.setFromAvatar(user.getAvatar());
        info.setFromName(user.getUserName());
        info.setOwnerId(param.getOwnerId());
        info.setType(param.getType());
        info.setLikeNum(0);
        return info;
    }

    public Boolean delete(CommentsInfoParam param) {
        if (Objects.isNull(param)){
            throw new BizException("非法操作，参数不能为空");
        }
        Long id = param.getId();
        if (Objects.isNull(id)){
            throw new BizException("非法操作，参数不能为空");
        }
        return commentsInfoService.removeById(id);
    }
}
